using System;
using UnityEngine;

[Serializable]
public class TTCameraBehaviourLookAtBall : TTCameraBehaviour
{
	public Vector3 offsetFromStart;

	public Vector3 lookAtOffset;

	public bool movePosition;

	public bool moveRotation;

	public bool slowDownTime;

	public float delay;

	public float timeScale = 0.2f;

	public float duration = 1f;

	public float totalDuration = 3f;

	protected TableTennisHumanPlayer humanPlayer;

	protected Vector3 startPosition;

	protected float time;

	public override void Start(Camera cam, TTCameraController camController, TTCameraOptions cameraOptions)
	{
		base.Start(cam, camController, cameraOptions);
		humanPlayer = cameraOptions.player as TableTennisHumanPlayer;
		startPosition = cameraOptions.player.match.ball.position;
		time = 0f;
	}

	public override void Update()
	{
		if (humanPlayer == null)
		{
			return;
		}
		float num = ((!cameraOptions.useDelay) ? delay : cameraOptions.delay);
		if (!humanPlayer.match.starter.paused)
		{
			float num2 = time;
			time += RealTime.deltaTime;
			if (time < num)
			{
				return;
			}
			if (num2 < num && time >= num)
			{
				startPosition = cameraOptions.player.match.ball.position;
			}
			if (slowDownTime)
			{
				if (time - num >= duration)
				{
					Time.timeScale = humanPlayer.match.starter.timeScale;
				}
				else
				{
					Time.timeScale = timeScale;
				}
			}
		}
		Vector3 vector = offsetFromStart;
		vector.z *= humanPlayer.tableSide;
		vector.x *= Mathf.Sign(startPosition.x);
		Vector3 vector2 = startPosition.OnGround() + vector;
		Vector3 position = humanPlayer.match.ball.position;
		Vector3 vector3 = position + lookAtOffset;
		Quaternion b = Quaternion.LookRotation(vector3 - vector2);
		if (movePosition)
		{
			camera.transform.position = Vector3.Lerp(camera.transform.position, vector2, RealTime.deltaTime * moveSpeed);
			if (moveRotation)
			{
				camera.transform.rotation = Quaternion.Lerp(camera.transform.rotation, b, RealTime.deltaTime * rotationSpeed);
			}
		}
		else if (moveRotation)
		{
			b = Quaternion.LookRotation(vector3 - camera.transform.position);
			camera.transform.rotation = Quaternion.Lerp(camera.transform.rotation, b, RealTime.deltaTime * rotationSpeed);
		}
		camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, fov, RealTime.deltaTime * rotationSpeed);
		if (time - num > totalDuration && cameraOptions.onDone != null)
		{
			cameraOptions.onDone();
		}
	}
}
